package com.example.myapplication_view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class FIWStepView extends View {
    private int mOuterColor = Color.YELLOW;
    private int mInnerColor = Color.BLUE;
    private int mBorderWidth;
    private int mStepTextColor = Color.BLUE;
    private int getmStepTextSize;
    // 这连个值在mainActivity.java 中设置
    private int mCurrentStep;  //  当前步数
    private int mMaxStep ;    // 最大步数

    private Paint mOuterPaint;// 外圆环 ，设置为全局
    private  Paint mInnerPaint; // 内圆环
    private Paint mTextpaint; // 文字

    // 重写get(),为了可以读到私有的值


    public int getmCurrentStep() {
        return mCurrentStep;
    }

    public void setmCurrentStep(int mCurrentStep) {
        this.mCurrentStep = mCurrentStep;
        // 防止调用后就停止，所以要刷新
        invalidate();
    }

    public int getmMaxStep() {
        return mMaxStep;
    }

    public void setmMaxStep(int mMaxStep) {
        this.mMaxStep = mMaxStep;
    }

    public FIWStepView(Context context) {
        // 把super 改成 this
        this(context,null);
    }

    public FIWStepView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FIWStepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 获取自定义属性
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.FIWStepView);
        mOuterColor = array.getColor(R.styleable.FIWStepView_outer_color,mOuterColor);
        mInnerColor = array.getColor(R.styleable.FIWStepView_inner_color,mInnerColor);
        mBorderWidth =(int)array.getDimension(R.styleable.FIWStepView_border_width,mBorderWidth);
        mStepTextColor=array.getColor(R.styleable.FIWStepView_step_text_color,mStepTextColor);
        getmStepTextSize=array.getDimensionPixelSize(R.styleable.FIWStepView_step_text_size,getmStepTextSize);
        array.recycle();

        //外圆环
        mOuterPaint = new Paint();
        mOuterPaint.setAntiAlias(true);
        mOuterPaint.setColor(mOuterColor);
        mOuterPaint.setStrokeWidth(mBorderWidth);
        //设置圆弧内不填充
        mOuterPaint.setStyle(Paint.Style.STROKE);
        //设置断点为圆弧
        mOuterPaint.setStrokeCap(Paint.Cap.ROUND);

        //内圆环
        mInnerPaint = new Paint();
        mInnerPaint.setAntiAlias(true);
        mInnerPaint.setColor(mInnerColor);
        mInnerPaint.setStrokeWidth(mBorderWidth);
        mInnerPaint.setStyle(Paint.Style.STROKE);
        mInnerPaint.setStrokeCap(Paint.Cap.ROUND);

        //  文字
        mTextpaint = new Paint();
        mTextpaint.setAntiAlias(true);
        mTextpaint.setColor(mStepTextColor);
        mTextpaint.setTextSize(getmStepTextSize);



    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 圆形由正方形截来，所以先以短边来成正方形
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width>height? height:width ,width>height?height:width);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 5 ---------绘制内圆弧
        // RectF rectF = new RectF(0,0,getWidth(),getWidth());
        // 新距离算法，保证圆环完整显示
        int center = getWidth()/2;
        int radius = (getWidth()-mBorderWidth)/2;
        RectF rectF = new RectF(mBorderWidth/2,mBorderWidth/2,center+radius,center+radius);
        //Paint mOuterPaint = new Paint();      这个耗时操作不要放在这里
        // 系统自带画圆弧的功能,需要参数
        canvas.drawArc(rectF,135,270,false,mOuterPaint);

        // ---------绘制外圆环
        if(mMaxStep ==0){return ;}
        float radio = (float) mCurrentStep/mMaxStep;
        canvas.drawArc(rectF,135,270*radio,false,mInnerPaint);


        //-------绘制文字
        String mText = mCurrentStep+"";
        Rect rect = new Rect();
        mTextpaint.getTextBounds(mText,0,mText.length(),rect);
        int dx= getWidth()/2-rect.width()/2;
        Paint.FontMetricsInt fontMetricsInt = mTextpaint.getFontMetricsInt();
        int dy = fontMetricsInt.bottom - fontMetricsInt.top;
        int baseLine = getHeight()/2-dy/2;
        canvas.drawText(mText,dx,baseLine,mTextpaint);



    }
}
